package com.neusoft.zqygbc.neusoft_wenda.fragment;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.jcodecraeer.xrecyclerview.XRecyclerView;
import com.neusoft.zqygbc.neusoft_wenda.R;
import com.neusoft.zqygbc.neusoft_wenda.adapter.CreateAnswerAdapter;
import com.neusoft.zqygbc.neusoft_wenda.base.RetrofitCallback;
import com.neusoft.zqygbc.neusoft_wenda.javeBean.AnswersEntity;
import com.neusoft.zqygbc.neusoft_wenda.javeBean.MyAnswerList;
import com.neusoft.zqygbc.neusoft_wenda.javeBean.QuestionDetail;
import com.neusoft.zqygbc.neusoft_wenda.model.AnswerModel;

import java.util.ArrayList;
import java.util.List;

import retrofit2.Call;

import static android.content.Context.MODE_PRIVATE;


/**
 * Created by dell on 2016/6/20.
 */
public class HisAnswerFragment extends Fragment {

    private XRecyclerView myanswer_xrv;
    private AnswerModel answerModel;
    private Call call;
    private RetrofitCallback<MyAnswerList> answersEntityRetrofitCallback;
    private Intent intent;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_answer, null);
        initView(view);
        initData();
        return view;
    }

    private void initView(View view) {
        intent = getActivity().getIntent();
        myanswer_xrv = (XRecyclerView) view.findViewById(R.id.myanswer_xrv);
        myanswer_xrv.setLayoutManager(new LinearLayoutManager(getActivity()));
    }

    private void initData() {
        answerModel = new AnswerModel();
        call = answerModel.myanswers(intent.getStringExtra("user_id"));
        answersEntityRetrofitCallback = new RetrofitCallback<MyAnswerList>() {
            @Override
            public void onResponse(MyAnswerList myAnswerList) {
                List<QuestionDetail> answerslist = myAnswerList.getQuestionDetail();
                if (answerslist != null) {
                    List<AnswersEntity> answersEntities1 = answerslist.get(1).getAnswers();
                    List<AnswersEntity> answersEntities2 = new ArrayList<>();
                    for(int i =0;i<answersEntities1.size();i++){
                        if(answersEntities1.get(i).getUid().equals(intent.getStringExtra("user_id"))){
                            answersEntities2.add(answersEntities1.get(i));

                        }
                    }
                    myanswer_xrv.setAdapter(new CreateAnswerAdapter(getActivity(), answersEntities2, R.layout.cardview_myanswer));
                }

            }
        };
        answerModel.bindCallback(call, answersEntityRetrofitCallback);

    }
}
